package com.julis.activity.setting.devicegongneng;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.Constant;
import com.baseutils.SpfUtils;
import com.github.czy1121.view.RoundButton;
import com.julis.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AreaActivity extends AppCompatActivity {
    List<String> area = new ArrayList<String>();
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.spinner_blood_pressure)
    Spinner spinnerBloodPressure;
    @BindView(R.id.logout)
    RoundButton logout;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    String st = "聚利";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area);
        ButterKnife.bind(this);
        String Areas = (String) SpfUtils.get(this, Constant.Area, "聚利");

        area.add("聚利");
        area.add("ITS");
        area.add("陕西");
        area.add("安徽");
        area.add("江苏");
        area.add("浙江");
        area.add("贵州");
        area.add("山东");
        area.add("河北");
        area.add("云南");
        area.add("福建");
        area.add("辽宁");
        area.add("军车");
        area.add("天津");
        area.add("湖南");
        area.add("总后");
        area.add("山西");


        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(AreaActivity.this,
                R.layout.item_select, area);
        spinnerAdapter.setDropDownViewResource(R.layout.item_drop);
        spinnerBloodPressure.setAdapter(spinnerAdapter);
        setSpinnerDefaultValue(spinnerBloodPressure, Areas);
        spinnerBloodPressure.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                st = area.get(pos);


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    /**
     * spinner 接收默认值的Spinner
     * value 需要设置的默认值
     */
    private void setSpinnerDefaultValue(Spinner spinner, String value) {
        SpinnerAdapter apsAdapter = spinner.getAdapter();
        int size = apsAdapter.getCount();
        for (int i = 0; i < size; i++) {
            if (TextUtils.equals(value, apsAdapter.getItem(i).toString())) {
                spinner.setSelection(i, true);
                break;
            }
        }
    }


    @OnClick({R.id.img_back, R.id.logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.logout:
                SpfUtils.put(this, Constant.Area, st);
                Toast.makeText(this, "地区设置成功！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
